#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：图灵学习 
@File    ：3.aiohttp的简单使用.py
@IDE     ：PyCharm 
@Author  ：ChenLiGao
@Date    ：2024-12-04 16:24
'''
import asyncio
import sys

import aiohttp

url = 'https://www.baidu.com'

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
}

async def get_baidu():
    # 使用上下文管理器的方式创建请求对象
    async with aiohttp.ClientSession() as session:
        async with session.get(url,headers=headers) as resp:
            response = await resp.text()
            print(resp.status)
            print(response)

# loop = asyncio.get_event_loop()
# loop.run_until_complete(get_baidu())

# windows 系统自行设置事件循环对象
# 加这一句是为了解决只有下面代码报RuntimeError: Event loop is closed错的问题，winows需要这样解决
if sys.platform == 'win32':
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(get_baidu())